/**
 * Copyright (C) 2011-2012 GreenSoft S.A.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *        http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ar.com.greensoft.neosystem.user.pf;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.inject.Inject;

import org.jboss.logging.Logger;

import ar.com.greensoft.jeecommons.web.jsf.util.FacesUtil;
import ar.com.greensoft.jeecommons.web.jsf.util.HttpJSFUtil;
import ar.com.greensoft.jeecommons.web.jsf.util.ResourceBundleUtil;
import ar.com.greensoft.neosystem.crm.web.beans.NavigationConstants;
import ar.com.greensoft.neosystem.dao.DaoException;
import ar.com.greensoft.neosystem.dao.PersonaFisicaDao;
import ar.com.greensoft.neosystem.model.Direccion;
import ar.com.greensoft.neosystem.model.PersonaFisica;
import ar.com.greensoft.neosystem.model.references.TipoDocumento;
import ar.com.greensoft.neosystem.util.CUITGenerator;

/**
 * 
 * @author Jorge E. Villaverde
 * @date 21/06/2013
 * @version 1.0.0
 */
@ManagedBean
@ViewScoped
public class CreatePersonaFisicaBean implements Serializable, NavigationConstants {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1052669946335306915L;
	
	private static final Logger logger = Logger.getLogger(CreatePersonaFisicaBean.class);

	@Inject
	private PersonaFisicaDao dao;

	private PersonaFisica personaFisica = new PersonaFisica();
	
	@PostConstruct
	public void init(){
		this.personaFisica.setTipoDocumento(TipoDocumento.DNI);
		this.personaFisica.setDireccion(new Direccion());
	}

	public String create(){
		try {
			if(logger.isTraceEnabled())
				logger.trace("Se intentará crear la persona física: " + personaFisica);
			
			personaFisica.setCuit(CUITGenerator.genertePersonaFisica(
					personaFisica.getSexo(),
					personaFisica.getNumeroDocumento()));
			
			dao.persist(personaFisica);
			
			if(logger.isTraceEnabled())
				logger.trace("Persona Fisica creada correctamente: " + personaFisica);

			String message = ResourceBundleUtil.getResourceBundleString("personaFisicaAddedMessage", HttpJSFUtil.getLocale());
			
			FacesUtil.addInfoMessage(message);
			
			return INDEX_ACTION;
		} catch (DaoException e) {
			logger.error(e.getMessage());
		}
		return null;
	}
	
	public PersonaFisica getPersonaFisica() {
		return personaFisica;
	}

	public void setPersonaFisica(PersonaFisica personaFisica) {
		this.personaFisica = personaFisica;
	}
}

